﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenTicket.Model;

namespace OpenTicket.Tests
{
    /// <summary>
    /// Summary description for OrderServiceTest
    /// </summary>
    [TestClass]
    public class OrderServiceTest:BaseTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void OpenTicketCanSaveNewOrder()
        {
            Model.Order o = new Model.Order();
            o.CreatedDate = DateTime.Now;
            o.OrderId = System.Guid.NewGuid();
            o.Client = new Client
            {
                ClientId = 1,
                FirstName = "John",
                LastName = "Doe"
            };
            o.OrderRows = new List<OrderRow>();
            ((IList<OrderRow>)o.OrderRows).Add(new OrderRow { OrderId = o.OrderId, Product = new Model.Product { ProductCost = 50000, ProductCode = "TestProduct", ProductName = "TestProduct" } });

            OrderService.Save(o);
          
        }

        [TestMethod]
        public void OpenTicketCanUpdateExistingOrder()
        {
            //Saves a new Order
            Model.Order o = new Model.Order();
            o.OrderId = System.Guid.NewGuid();
            o.CreatedDate = DateTime.Now;
            o.Client = new Client{ClientId = 1, FirstName = "John", LastName = "Doe"};
            o.OrderRows = new List<OrderRow>();
            ((IList<OrderRow>)o.OrderRows).Add(new OrderRow { Product = new Model.Product { ProductCost = 50000, ProductCode = "TestProduct", ProductName = "TestProduct" } });            
            OrderService.Save(o);

            //Check to see the order is still the same
            Assert.AreEqual(o.OrderRows.First().Product.ProductName, "TestProduct");

            //Change Amount
            o.OrderRows.First().Amount = 5;
            OrderService.Save(o);

            //Load it again in a new 
            OrderService.GetOrderById(o.OrderId);
           
            //Check values
            Assert.AreEqual(o.OrderId, o.OrderId);
            Assert.AreEqual("TestProduct", o.OrderRows.First().Product.ProductName);
            Assert.AreEqual(5, o.OrderRows.First().Amount);
           
        }

        [TestMethod]
        public void OpenTicketCanGetOrderById()
        {

            var o2 = OrderService.GetOrderById(new Guid());
            Assert.AreEqual(new Guid(), o2.OrderId);
            Assert.AreEqual(1, o2.OrderRows.Count());
            Assert.AreEqual("TestProduct", o2.OrderRows.First().Product.ProductName);
        }

        [TestMethod]
        public void OpenTicketCanDeleteOneOrder()
        {
            var guid = System.Guid.NewGuid();
            //Save the test order
            Model.Order o = new Model.Order();
            o.CreatedDate = DateTime.Now;
            o.OrderId = guid;
            o.Client = new Client { ClientId = 1, FirstName = "John", LastName = "Doe" };
            o.OrderRows = new List<OrderRow>();
            ((IList<OrderRow>)o.OrderRows).Add(new OrderRow { OrderId = new Guid(), Product = new Model.Product { ProductCost = 50000, ProductCode = "TestProduct", ProductName = "Changed" } });
            Model.Order o2 = OrderService.Save(o);

            //Delete it
            OrderService.DeleteOrder(new Model.Order() { OrderId = guid });
            //Assert it´s gone
            Assert.IsNull(OrderService.GetOrderById(guid));
        }

            
    }
}
